package com.yanqu.road.server.cmd.activity.apprentice;

import com.yanqu.road.entity.activity.apprentice.ApprenticeConvertResult;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ApprenticeProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.apprentice.ApprenticeModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

@CmdApi(reqMsg = ApprenticeProto.ApprenticeTalentConvertPropReqMsg.class, respMsg = ApprenticeProto.ApprenticeTalentConvertPropRespMsg.class)
@Cmd(code = Protocol.S_APPRENTICE_TALENT_CONVERT_PROP,desc = "爱徒养成-天资转换道具（玩家维度）")
public class ApprenticeTalentConvertPropCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ApprenticeProto.ApprenticeTalentConvertPropReqMsg reqMsg = ApprenticeProto.ApprenticeTalentConvertPropReqMsg.parseFrom(packet.getBytes());
        ApprenticeProto.ApprenticeTalentConvertPropRespMsg.Builder respMsg = ApprenticeProto.ApprenticeTalentConvertPropRespMsg.newBuilder();
        try {
            //天资转换道具（玩家维度）
            ApprenticeConvertResult result = player.getModule(ApprenticeModule.class).convertTalentToProp(reqMsg.getMallShopType());
            respMsg.setRet(0);
            respMsg.setReward(PropertyHelper.parsePropertyToString(result.getReward()));
            for (Map.Entry<Integer, Long> entry : result.getTalentCostMap().entrySet()) {
                ApprenticeProto.ApprenticeTalentData.Builder talentBuilder = ApprenticeProto.ApprenticeTalentData.newBuilder();
                talentBuilder.setTalentType(entry.getKey());
                talentBuilder.setTalentValue(entry.getValue());
                respMsg.addCostTalent(talentBuilder);
            }
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        player.sendPacket(Protocol.U_APPRENTICE_TALENT_CONVERT_PROP, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {}
}
